package com.ebt.m.customer.task;

import android.content.Context;
import android.text.TextUtils;

import com.ebt.m.AppContext;
import com.ebt.m.customer.model.CustomerModel;
import com.ebt.m.customer.model.CustomerModelDetail;
import com.ebt.m.customer.util.CompareHelper;
import com.ebt.m.customer.util.CustomerConvertUtil;
import com.ebt.m.data.CustomerData;
import com.ebt.m.data.entity.Customer;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;

/**
 * by rick.wang
 */
public class CustomerDataGenerator {

    private static CustomerDataGenerator instance;

    private CustomerDataGenerator() {
    }

    public static CustomerDataGenerator getInstance() {

        if (instance == null) {
            synchronized (CustomerDataGenerator.class) {
                if (instance == null) {
                    instance = new CustomerDataGenerator();
                }
            }
        }
        return instance;
    }

    public CustomerModelPot genCustomerModelPot(Context context, Date dateFrom, Date dateTo) {
        CustomerModelPot pot = null;
        try {
            pot = new CustomerModelPot();
            CustomerData customerData = new CustomerData(context);
            List<Customer> customers = null;
            if (AppContext.getCurrentUser().isLogined()) {
                customers = customerData.getListOfCustomersByIdentity(AppContext.getCurrentUser().getUserId(), true);
            } else {
                customers = customerData.getListOfCustomersByIdentity(0, true);
            }
            List<CustomerModel> customerModels = CustomerConvertUtil.convertListCustomerToCustomerModelGeneral(customers);
            pot.generals = customerModels;
            pot.details = getAllCustomerModelDetails(customerData, dateFrom, dateTo);

        } catch (Exception e) {
            e.printStackTrace();
        }
        return pot;
    }

    public List<CustomerModel> genCustomerModels(Context context) {
        List<CustomerModel> customerModels = null;
        try {
            CustomerData customerData = new CustomerData(context);
            List<Customer> customers;
            if (AppContext.getCurrentUser().isLogined()) {
                customers = customerData.getListOfCustomersByIdentity(AppContext.getCurrentUser().getUserId(), true);
            } else {
                customers = customerData.getListOfCustomersByIdentity(0, true);
            }
            customerModels = CustomerConvertUtil.convertListCustomerToCustomerModelGeneral(customers);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return customerModels;
    }

    // 1.需要进行一下排序
    public synchronized List<CustomerModelDetail> getAllCustomerModelDetails(CustomerData customerData, Date dateFrom, Date dateTo) {
        List<CustomerModelDetail> list = new ArrayList<>();
        //1.获取 CustomerModelDetail.TYPE_NEW
        list.addAll(customerData.getListOfCustomerNewInDateRegion(dateFrom, dateTo));
        //2.获取 CustomerModelDetail.TYPE_PROPOSAL
//        list.addAll(customerData.getListOfCustomerProposalInDateRegion(dateFrom, dateTo));
        list.addAll(customerData.getListOfCustomerProposalInDateRegion2(dateFrom, dateTo));
        //3.获取 CustomerModelDetail.TYPE_INTOUCH  6  8
        list.addAll(customerData.getListOfCustomerIntouchInDateRegion(dateFrom, dateTo));
        //去重
        if (list != null) {
            Collections.sort(list, CompareHelper.COMPARATOR_CUSTOMER_MODEL_DETAIL_UUID);
            list = removeSameUuidCustomer(list);
            Collections.sort(list, CompareHelper.COMPARATOR_CUSTOMER_MODEL_DETAIL_DATE);
        }
        return list;
    }

    private List<CustomerModelDetail> removeSameNameCustomer(List<CustomerModelDetail> input) {
        if (input == null) {
            return null;
        }
        List<CustomerModelDetail> output = new ArrayList<CustomerModelDetail>();
        CustomerModelDetail prevItem = null;
        for (CustomerModelDetail currItem : input) {
            if (compareCustomerModelDetailName(prevItem, currItem)) {
                prevItem = currItem;
                continue;
            } else {
                prevItem = currItem;
                output.add(currItem);
            }
        }
        return output;
    }

    private List<CustomerModelDetail> removeSameUuidCustomer(List<CustomerModelDetail> input) {
        if (input == null) {
            return null;
        }
        List<CustomerModelDetail> output = new ArrayList<CustomerModelDetail>();
        CustomerModelDetail prevItem = null;
        for (CustomerModelDetail currItem : input) {
            if (compareCustomerModelDetailUuid(prevItem, currItem)) {
                prevItem = currItem;
                continue;
            } else {
                prevItem = currItem;
                output.add(currItem);
            }
        }
        return output;
    }

    private boolean compareCustomerModelDetailName(CustomerModelDetail a, CustomerModelDetail b) {
        if (a == null && b == null) {
            return true;
        }
        if (a != null && b != null) {
            if (a.name != null && a.name.equals(b.name)) {
                return true;
            } else {
                return false;
            }
        }
        return false;
    }

    private boolean compareCustomerModelDetailUuid(CustomerModelDetail a, CustomerModelDetail b) {
        if (a == null && b == null) {
            return true;
        }
        if (a != null && b != null) {
            if (a.getCustomerUuid() != null && a.getCustomerUuid().equals(b.getCustomerUuid())) {
                return true;
            } else {
                return false;
            }
        }
        return false;
    }

    public static class CustomerModelPot {
        public List<CustomerModel> generals;
        public List<CustomerModelDetail> details;
    }

    /**
     * 获取sort key的首个字符，如果是英文字母就直接返回，否则返回#。
     *
     * @param sortKeyString 数据库中读取出的sort key
     * @return 英文字母或者#
     */
    private char getSortKey(String sortKeyString) {
        if (!TextUtils.isEmpty(sortKeyString)) {
            char c = sortKeyString.charAt(0);
            if (c >= 97 && c <= 122) {
                return (char) (c - 32);
            }
            if (c >= 65 && c <= 90) {
                return c;
            }
        }
        return '#';
    }
}